package com.my.swas.photo.controller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.my.swas.common.base.ResponseResult;
import com.my.swas.photo.config.PhotoYmlConfig;
import com.my.swas.photo.entity.UserInfo;
import com.my.swas.photo.entity.enums.PartnerTypeEnum;
import com.my.swas.photo.mapper.UserMapper;
import com.my.swas.photo.service.AlipayUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Description:
 * @author: :MaYong
 */
@Api(value = "用户接口",tags = "用户接口",description = "用户获取，token获取")
@RestController
@RequestMapping("userinfo")
@Slf4j
public class AlipayUserInfoController {
    @Resource
    AlipayUserInfoService alipayUserInfoService;
    @Resource
    PhotoYmlConfig photoYmlConfig;
    @Resource
    UserMapper userMapper;
    @ApiOperation(value = "获取用户信息")
    @GetMapping("alipayUserInfo")
    public ResponseResult alipayUserInfo(@ApiParam(name = "authCode", value = "获取的权限Code", required = true) @RequestParam("authCode") String authCode
            , @ApiParam(name = "chooseColor", value = "图片背景", required = true) @RequestParam("chooseColor") String chooseColor
            , @ApiParam(name = "chooseImage", value = "图片路径", required = true) @RequestParam("chooseImage") String chooseImage) throws Exception {

/*        AlipaySystemOauthTokenResponse alipaySystemOauthTokenResponse = alipayUserInfoService.alipayUserInfo(authCode);
        UserInfo userInfo = UserInfo.builder()
                .partnerUserId(alipaySystemOauthTokenResponse.getAlipayUserId())
                .build();*/
        String tempDir = photoYmlConfig.getMy().getTempdir() + File.separator;
        String path = chooseImage.replaceAll("https://my.crytst.com/image/","");
        String tempFile = tempDir + path;

        String name = path.substring(0,path.lastIndexOf("."))+"_"+chooseColor;
        String preFix = chooseImage.substring(chooseImage.lastIndexOf("."));
        String baseDir = tempDir + name + preFix;
        String url = "https://my.crytst.com/image/"+name+preFix;
        log.info("文件地址："+tempFile);
        File file = new File(tempFile);
        String diTu;
        switch (chooseColor){
            case "red":
                diTu = "http://img.crytst.com/red.png";
                break;
            case "blue":
                diTu = "http://img.crytst.com/blue.png";
                break;
            default:
                diTu = "http://img.crytst.com/white.png";
        }

        BufferedImage read = ImgUtil.read(file);
        log.info("底色:"+chooseColor);
        File diBan = new File(tempDir+read.getWidth()+"x"+read.getHeight()+diTu.substring(diTu.lastIndexOf("/")+1));

        log.info("底图：{}",diTu);
        File to = new File(baseDir);
        Thumbnails.of(new URL(diTu)).sourceRegion(Positions.CENTER,read.getWidth(),read.getHeight()).size(read.getWidth(), read.getHeight()).toFile(diBan);
        Thumbnails.of(diBan).size(read.getWidth(),read.getHeight()).watermark(Positions.CENTER, ImageIO.read(file), 1f)
                .outputQuality(1f).toFile(to);
        return ResponseResult.success(url);
    }

    @GetMapping("add")
    public ResponseResult addUserId() {
        UserInfo userInfo = UserInfo.builder()
                .partnerUserId("123")
                .partnerNickName("355")
                .partnerType(PartnerTypeEnum.ALIPAY)
                .build();
        userInfo.toBuilder().partnerAvatar("123").build();
        return ResponseResult.success(userInfo);
    }


}
